home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 825 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: fresl@master.grad.hr (Kresimir Fresl)
  3. Newsgroups: comp.std.c++
  4. Subject: fstream constructor and strings
  5. Date: 22 Mar 1996 10:17:28 PST
  6. Organization: ?
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <9603221244.AA07167@master.grad.hr>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: Fri, 22 Mar 96 13:44:10 +0100
  11. Content-Length: 1338
  12. X-Lines: 41
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMVLvI0y4NqrwXLNJAQGkBAH/f0rM1U+gWdKJXy4486RmUOgbGyQ5CAOb
  15.     DyZ/3/2L86MLLcrbDfqaPOXTYpFc/V7mp1KVYbb5CfgZ0TPx6CteOA==
  16.     =oZXn
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. I think that the following `idiom' was often used:
  20.     
  21.     // necessary includes
  22.     
  23.     cout >> "Enter file name -> ";
  24.     String file_name;
  25.     ifstream (file_name); // or: ofstream (file_name);
  26.     
  27. (I used `S' (not `s') because this is not a proposed standard string 
  28. class.) 
  29.  
  30. ifstream constructor expects const char*, but above call was possible 
  31. because String classes (eg. in libg++) used to have:
  32.     String::operator const char*() const
  33. (or something similar). 
  34.  
  35. According to proposed standard, one should write:
  36.     ifstream (file_name.c_str());
  37. because implicit conversion of string to const char* is not defined.
  38.  
  39. I agree that implicit conversion is (almost always) dangerous, and I 
  40. _do not_ propose to add conversion operator to string class. Instead, I
  41. think that fstream classes (beside, or even instead of, constructors that 
  42. take const char*s) should also have constructors that take strings.
  43. In many C++ books one can read that plain C arrays (including char[]s and
  44. char*s) are to low level, and that with classes (eg. vectors and strings) 
  45. one can work on higher levels of abstraction. fstreams are `better' than
  46. cstdio functions, but, because of const char* parameters, they are still
  47. to low level. 
  48.  
  49.  
  50. fresl
  51.  
  52. -----------
  53. Kresimir Fresl
  54. Faculty of Civil Engineering, Zagreb, Croatia
  55. email: fresl@master.grad.hr
  56. ---
  57. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  58.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  59.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  60.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  61.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  62. ]
  63.